import {Component, EventEmitter, Input, OnChanges, OnInit, Output, SimpleChange} from "@angular/core";
import {CommService} from "../comm-service";
import {MyhttpService} from "../myhttp.service";
import {SharedService} from "../shared-service";

declare var $: any;

@Component({
  selector: "video-tree",
  template: `
    <div id="videotree" class="videotree"></div>
  `,
  styles: [
    ".videotree{width: auto}",
  ]
})
export class VideoTreeComponent implements OnInit, OnChanges {
  treetype: string;
  s_ksjhbh: string;
  s_ksccbh: string;
  frompage: string;

  /**
   *  场所树-----------------------------------
   * @param {string} flag
   * "" ----初始化构造
   * "refresh"---刷新场所树
   *
   * */
  initcstree(flag: string): void {
    this.frompage = this.o.frompage;
    if (flag === "refresh") {
      $("#videotree").jstree("refresh");
      return;
    }
    const nodelx = this.o.nodelx;
    const cxlxarr = nodelx.split("_");
    const cxlx = cxlxarr.join(",");
    this.createtree_cs(cxlx);
  }

  /**
   * 逗号隔开， 可选值  ksgljg,bzhkd,bms,zhzx
   * @param {string} cxlx
   */
  createtree_cs(cxlx: string): void {
    const that = this;
    $("#videotree").jstree({
      "core": {
        "themes": {
          //如果使用 false默认主题则保留
          name: "default-dark",
          // 主题的CSS文件的URL，请保留该属性，就 false好像您已手动包含主题CSS（推荐）一样。您也可以将此设置为true 将尝试自动加载主题
          url: true,
          //所有jstree主题的位置-仅当 url 设置为时使用 true
          // 最终加载的css文件是  assets/jstreethemes/ 上面配置的name属性的值 /style.css【这里面的样式名要换成 上面配置的name属性的值】
          dir: "assets/jstreethemes",
          //指示是否显示连接点的布尔值
          dots: true,
          // 指示是否显示节点图标的布尔值
          icons: true,
          // 指示是否应显示节点省略号的布尔值-只能与容器上的固定with一起使用
          ellipsis: false,
          // 指示树背景是否为条纹的布尔值
          stripes: false,
          // 一个字符串（或boolean false），指定要使用的主题变体（如果主题支持变体）
          variant: false,
          //一个布尔值，指定主题的响应版本是否应在较小的屏幕上显示（如果主题支持）。默认为false.
          responsive: true,
        },
        data: function (node, cb) {
          if (node.id === "#") {
            that.cstreeNode(that.o.id, that.o.type, cxlx, (data) => {
              // 根节点信息从 登入用户信息来
              const rootNode = {
                id: that.o.id,
                type: that.o.type,
                text: that.o.text,
                data: {},
                state: {
                  opened: true
                },
                children: data
              };
              cb(rootNode);
            });
          } else {
            that.cstreeNode(node.id, node.type, cxlx, (data) => {
              cb(data);
            });
          }
        }
      },
      "plugins": ["types"],
      "types": {
        "ksgljg": {
          "icon": "glyphicon glyphicon-home"
        },
        "bzhkd": {
          "icon": "fa fa-university"
        },
        "bms": {
          "icon": "fa fa-shield"
        },
        "zhzx": {
          "icon": "fa fa-flag"
        }
      }
    }).on("select_node.jstree", function (e, selected) {
      const nodeType = selected.node.type;
      const nodeid = selected.node.id;  //csid
      that.sharedService.emitChange({
        actiontype: "clicknode",
        frompage: that.frompage,
        data: {
          nodeid: nodeid,
          nodetype: nodeType
        }
      });
    }).on("loaded.jstree", function () {
      if (that.o["clickroot"] === false) {
        return
      }
      const rootnodeid: string = that.o["id"];
      const rootnode = $("#videotree").jstree('get_node', rootnodeid);
      $("#videotree").jstree('activate_node', rootnode);//代码点击根节点
    });
  }

  /**
   *  场所业务树-----------------------------------
   * @param {string} flag
   * "" ----初始化构造
   * "refresh"---刷新场所树
   *
   * */
  initcsywtree(flag: string): void {
    this.frompage = this.o.frompage;
    if (flag == "refresh") {
      $("#videotree").jstree("refresh");
      return;
    }
    const nodelx = this.o.nodelx;
    switch (nodelx) {
      case "ksgljg_bms_jt":
        //保密室视频回放 Z1 Z2 Z3    ----保密室巡查 Z1 Z2 Z3
        this.createtree_ksgljg_bms_jt();
        break;
      case "bms_jt":
        // 保密室视频回放  M      ---- 保密室巡查 M
        this.createtree_bms_jt();
        break;
      case "ksgljg_bms":
        // 保密室巡查 Z1 Z2 Z3
        //this.createtree_ksgljg_bms();
        break;
      case "bms":
        // 保密室巡查 M
        // this.createtree_bms();
        break;
    }
  }


  /**
   * 节点是 ksgljg bms jt, 其中   jt是子节点
   */
  createtree_ksgljg_bms_jt(): void {
    const that = this;
    $("#videotree").jstree({
      "core": {
        "themes": {
          //如果使用 false默认主题则保留
          name: "default-dark",
          // 主题的CSS文件的URL，请保留该属性，就 false好像您已手动包含主题CSS（推荐）一样。您也可以将此设置为true 将尝试自动加载主题
          url: true,
          //所有jstree主题的位置-仅当 url 设置为时使用 true
          // 最终加载的css文件是  assets/jstreethemes/ 上面配置的name属性的值 /style.css【这里面的样式名要换成 上面配置的name属性的值】
          dir: "assets/jstreethemes",
          //指示是否显示连接点的布尔值
          dots: true,
          // 指示是否显示节点图标的布尔值
          icons: true,
          // 指示是否应显示节点省略号的布尔值-只能与容器上的固定with一起使用
          ellipsis: false,
          // 指示树背景是否为条纹的布尔值
          stripes: false,
          // 一个字符串（或boolean false），指定要使用的主题变体（如果主题支持变体）
          variant: false,
          //一个布尔值，指定主题的响应版本是否应在较小的屏幕上显示（如果主题支持）。默认为false.
          responsive: true,
        },
        data: function (node, cb) {
          if (node.id === "#") {
            that.ksgljg_bmsNode(that.o.id, that.o.type, false, (data) => {
              // 根节点信息从 登入用户信息来
              const rootNode = {
                id: that.o.id,
                type: that.o.type,
                text: that.o.text,
                data: {},
                state: {
                  opened: true
                },
                children: data
              };
              cb(rootNode);
            });
          } else {
            const nodetype = node.type;
            if (nodetype === "ksgljg") {
              that.ksgljg_bmsNode(node.id, node.type, false, (data) => {
                cb(data);
              });
            } else if (nodetype == "bms") {
              that.bmsjtNode(node.id, true, (data) => {
                cb(data);
              });
            }
          }
        }
      },
      "plugins": ["types"],
      "types": {
        "ksgljg": {
          "icon": "glyphicon glyphicon-home"
        },
        "bms": {
          "icon": "fa fa-shield"
        },
        "jt": {
          "icon": "fa fa-video-camera"
        }
      }
    }).on("select_node.jstree", function (e, selected) {
      const nodeType = selected.node.type;
      const nodeid = selected.node.id;  //csid
      const nodetext = selected.node.text;
      const bmsbh = $("#videotree").jstree("get_parent", nodeid);
      const bmsnode = $('#videotree').jstree('get_node', bmsbh);
      if (nodeType === "jt") {
        that.sharedService.emitChange({
          actiontype: "clicknode",
          frompage: that.frompage,
          data: {
            nodeid: nodeid,
            nodetext: nodetext,
            nodetype: nodeType,
            sipurl: selected.node.data.sipurl,
            bmsbh: bmsbh,
            bmsmc: bmsnode.text
          }
        });
      }
    });
  }

  /**
   * 节点是 bms/jt  jt是子节点
   */
  createtree_bms_jt(): void {
    const that = this;
    $("#videotree").jstree({
      "core": {
        "themes": {
          //如果使用 false默认主题则保留
          name: "default-dark",
          // 主题的CSS文件的URL，请保留该属性，就 false好像您已手动包含主题CSS（推荐）一样。您也可以将此设置为true 将尝试自动加载主题
          url: true,
          //所有jstree主题的位置-仅当 url 设置为时使用 true
          // 最终加载的css文件是  assets/jstreethemes/ 上面配置的name属性的值 /style.css【这里面的样式名要换成 上面配置的name属性的值】
          dir: "assets/jstreethemes",
          //指示是否显示连接点的布尔值
          dots: true,
          // 指示是否显示节点图标的布尔值
          icons: true,
          // 指示是否应显示节点省略号的布尔值-只能与容器上的固定with一起使用
          ellipsis: false,
          // 指示树背景是否为条纹的布尔值
          stripes: false,
          // 一个字符串（或boolean false），指定要使用的主题变体（如果主题支持变体）
          variant: false,
          //一个布尔值，指定主题的响应版本是否应在较小的屏幕上显示（如果主题支持）。默认为false.
          responsive: true,
        },
        data: function (node, cb) {
          if (node.id === "#") {
            const rootNode = {
              id: that.o.id,
              type: that.o.type,
              text: that.o.text,
              data: {},
              children: true
            };
            cb(rootNode);
          } else {
            that.bmsjtNode(node.id, true, (data) => {
              cb(data);
            });
          }
        }
      },
      "plugins": ["types"],
      "types": {
        "bms": {
          "icon": "fa fa-shield"
        },
        "jt": {
          "icon": "fa fa-video-camera"
        }
      }
    }).on("select_node.jstree", function (e, selected) {
      const nodeType = selected.node.type;
      const nodeid = selected.node.id;  //csid
      const nodetext = selected.node.text;
      const bmsbh = $("#videotree").jstree("get_parent", nodeid);
      const bmsnode = $('#videotree').jstree('get_node', bmsbh);
      if (nodeType === "jt") {
        that.sharedService.emitChange({
          actiontype: "clicknode",
          frompage: that.frompage,
          data: {
            nodeid: nodeid,
            nodetext: nodetext,
            nodetype: nodeType,
            sipurl: selected.node.data.sipurl,
            bmsbh: bmsbh,
            bmsmc: bmsnode.text
          }
        });
      }
    });
  }


  /**
   * 节点是   ksgljg/bms  bms是子节点
   */
  createtree_ksgljg_bms(): void {
    const that = this;
    $("#videotree").jstree({
      "core": {
        "themes": {
          //如果使用 false默认主题则保留
          name: "default-dark",
          // 主题的CSS文件的URL，请保留该属性，就 false好像您已手动包含主题CSS（推荐）一样。您也可以将此设置为true 将尝试自动加载主题
          url: true,
          //所有jstree主题的位置-仅当 url 设置为时使用 true
          // 最终加载的css文件是  assets/jstreethemes/ 上面配置的name属性的值 /style.css【这里面的样式名要换成 上面配置的name属性的值】
          dir: "assets/jstreethemes",
          //指示是否显示连接点的布尔值
          dots: true,
          // 指示是否显示节点图标的布尔值
          icons: true,
          // 指示是否应显示节点省略号的布尔值-只能与容器上的固定with一起使用
          ellipsis: false,
          // 指示树背景是否为条纹的布尔值
          stripes: false,
          // 一个字符串（或boolean false），指定要使用的主题变体（如果主题支持变体）
          variant: false,
          //一个布尔值，指定主题的响应版本是否应在较小的屏幕上显示（如果主题支持）。默认为false.
          responsive: true,
        },
        data: function (node, cb) {
          if (node.id === "#") {
            that.ksgljg_bmsNode(that.o.id, that.o.type, true, (data) => {
              // 根节点信息从 登入用户信息来
              const rootNode = {
                id: that.o.id,
                type: that.o.type,
                text: that.o.text,
                data: {},
                state: {
                  opened: true
                },
                children: data
              };
              cb(rootNode);
            });
          } else {
            that.ksgljg_bmsNode(node.id, node.type, true, (data) => {
              cb(data);
            });
          }
        }
      },
      "plugins": ["types"],
      "types": {
        "ksgljg": {
          "icon": "glyphicon glyphicon-home"
        },
        "bms": {
          "icon": "fa fa-shield"
        }
      }
    }).on("select_node.jstree", function (e, selected) {
      const nodeType = selected.node.type;
      const nodeid = selected.node.id;  //csid
      if (nodeType === "bms") {
        that.sharedService.emitChange({
          actiontype: "clicknode",
          frompage: that.frompage,
          data: {
            nodeid: nodeid,
            nodetype: nodeType
          }
        });
      }
    })
  }

  /**
   * 节点是  bms   bms是子节点
   */
  createtree_bms(): void {
    const that = this;
    $("#videotree").jstree({
      "core": {
        "themes": {
          //如果使用 false默认主题则保留
          name: "default-dark",
          // 主题的CSS文件的URL，请保留该属性，就 false好像您已手动包含主题CSS（推荐）一样。您也可以将此设置为true 将尝试自动加载主题
          url: true,
          //所有jstree主题的位置-仅当 url 设置为时使用 true
          // 最终加载的css文件是  assets/jstreethemes/ 上面配置的name属性的值 /style.css【这里面的样式名要换成 上面配置的name属性的值】
          dir: "assets/jstreethemes",
          //指示是否显示连接点的布尔值
          dots: true,
          // 指示是否显示节点图标的布尔值
          icons: true,
          // 指示是否应显示节点省略号的布尔值-只能与容器上的固定with一起使用
          ellipsis: false,
          // 指示树背景是否为条纹的布尔值
          stripes: false,
          // 一个字符串（或boolean false），指定要使用的主题变体（如果主题支持变体）
          variant: false,
          //一个布尔值，指定主题的响应版本是否应在较小的屏幕上显示（如果主题支持）。默认为false.
          responsive: true,
        },
        data: function (node, cb) {
          if (node.id === "#") {
            const rootNode = {
              id: that.o.id,
              type: that.o.type,
              text: that.o.text,
              data: {},
              state: {
                opened: true
              },
              children: false
            };
            cb(rootNode);
          }
        }
      },
      "plugins": ["types"],
      "types": {
        "bms": {
          "icon": "fa fa-shield"
        }
      }
    }).on("select_node.jstree", function (e, selected) {
      const nodeType = selected.node.type;
      const nodeid = selected.node.id;  //csid
      if (nodeType === "bms") {
        that.sharedService.emitChange({
          actiontype: "clicknode",
          frompage: that.frompage,
          data: {
            nodeid: nodeid,
            nodetype: nodeType
          }
        });
      }
    })
  }


  /**
   *  业务树， 和考试计划和考试场次有关系-----------------------------------
   * @param {string} flag
   * "" ----初始化构造
   * "refresh"---刷新场所树
   *
   * */
  initywtree(flag: string): void {
    this.frompage = this.o.frompage;
    if (flag === "refresh") {
      $("#videotree").jstree("refresh");
      return;
    }
    if (!this.s_ksjhbh || !this.s_ksccbh) {
      return
    }
    const nodelx = this.o.nodelx;
    switch (nodelx) {
      case "ksgljg_kd":
        this.createtree_ksgljg_kd();
        break;
      case "ksgljg_kd_kc":
        // 考点巡查 Z1 Z2 Z3
        this.createtree_ksgljg_kd_kc();
        break;
      case "kd_kc":
        // 考点巡查  K
        this.createtree_kd_kc();
        break;
      case "ksgljg_kd_kc_jt":
        // 考场视频回放 Z1 Z2 Z3
        this.createtree_ksgljg_kd_kc_jt();
        break;
      case "kd_kc_jt":
        // 考场视频回放 K
        this.createtree_kd_kc_jt();
        break;
    }
  }

  /**
   * 节点是   ksgljg/kd   kd是子节点
   */
  createtree_ksgljg_kd(): void {
    const that = this;
    $("#videotree").jstree({
      "core": {
        "themes": {
          //如果使用 false默认主题则保留
          name: "default-dark",
          // 主题的CSS文件的URL，请保留该属性，就 false好像您已手动包含主题CSS（推荐）一样。您也可以将此设置为true 将尝试自动加载主题
          url: true,
          //所有jstree主题的位置-仅当 url 设置为时使用 true
          // 最终加载的css文件是  assets/jstreethemes/ 上面配置的name属性的值 /style.css【这里面的样式名要换成 上面配置的name属性的值】
          dir: "assets/jstreethemes",
          //指示是否显示连接点的布尔值
          dots: true,
          // 指示是否显示节点图标的布尔值
          icons: true,
          // 指示是否应显示节点省略号的布尔值-只能与容器上的固定with一起使用
          ellipsis: false,
          // 指示树背景是否为条纹的布尔值
          stripes: false,
          // 一个字符串（或boolean false），指定要使用的主题变体（如果主题支持变体）
          variant: false,
          //一个布尔值，指定主题的响应版本是否应在较小的屏幕上显示（如果主题支持）。默认为false.
          responsive: true,
        },
        data: function (node, cb) {
          if (node.id === "#") {
            that.ksgljgkdNode("createtree_ksgljg_kd", that.o.id, true, (data) => {
              const rootNode = {
                id: that.o.id,
                type: that.o.type,
                text: that.o.text,
                data: {},
                state: {
                  opened: true
                },
                children: data
              };
              cb(rootNode);
            });
          } else {
            that.ksgljgkdNode("createtree_ksgljg_kd", node.id, true, (data) => {
              cb(data);
            });
          }
        }
      },
      "plugins": ["types"],
      "types": {
        "ksgljg": {
          "icon": "glyphicon glyphicon-home"
        },
        "kd": {
          "icon": "fa fa-university"
        }
      }
    }).on("select_node.jstree", function (e, selected) {
      const nodeType = selected.node.type;
      const nodeid = selected.node.id; //ksgljgid / kdbh
      that.sharedService.emitChange({
        actiontype: "clicknode",
        frompage: that.frompage,
        data: {
          nodeid: nodeid,
          nodetype: nodeType,
          bzhkdid: selected.node.data.bzhkdid
        }
      });
    })
  }

  /**
   * 节点是   ksgljg/kd/kc    kc 是子节点
   */
  createtree_ksgljg_kd_kc(): void {
    const that = this;
    $("#videotree").jstree({
      "core": {
        "themes": {
          //如果使用 false默认主题则保留
          name: "default-dark",
          // 主题的CSS文件的URL，请保留该属性，就 false好像您已手动包含主题CSS（推荐）一样。您也可以将此设置为true 将尝试自动加载主题
          url: true,
          //所有jstree主题的位置-仅当 url 设置为时使用 true
          // 最终加载的css文件是  assets/jstreethemes/ 上面配置的name属性的值 /style.css【这里面的样式名要换成 上面配置的name属性的值】
          dir: "assets/jstreethemes",
          //指示是否显示连接点的布尔值
          dots: true,
          // 指示是否显示节点图标的布尔值
          icons: true,
          // 指示是否应显示节点省略号的布尔值-只能与容器上的固定with一起使用
          ellipsis: false,
          // 指示树背景是否为条纹的布尔值
          stripes: false,
          // 一个字符串（或boolean false），指定要使用的主题变体（如果主题支持变体）
          variant: false,
          //一个布尔值，指定主题的响应版本是否应在较小的屏幕上显示（如果主题支持）。默认为false.
          responsive: true,
        },
        data: function (node, cb) {
          if (node.id === "#") {
            that.ksgljgkdNode("createtree_ksgljg_kd", that.o.id, false, (data) => {
              const rootNode = {
                id: that.o.id,
                type: that.o.type,
                text: that.o.text,
                data: {},
                state: {
                  opened: true
                },
                children: data
              };
              cb(rootNode);
            });
          } else {
            const nodetype = node.type;
            if (nodetype === "ksgljg") {
              that.ksgljgkdNode("createtree_ksgljg_kd", node.id, false, (data) => {
                cb(data);
              });
            } else if (nodetype === "kd") {
              that.kcNode(node.id, true, (data) => {
                cb(data);
              });
            }
          }
        }
      },
      "plugins": ["types"],
      "types": {
        "ksgljg": {
          "icon": "glyphicon glyphicon-home"
        },
        "kd": {
          "icon": "fa fa-university"
        },
        "kc": {
          "icon": "fa fa-graduation-cap"
        }
      }
    }).on("select_node.jstree", function (e, selected) {
      const nodeType = selected.node.type;
      const nodeid = selected.node.id;  // 如果节点是 kc, 那么  nodeid是 ""
      const parentid = $("#videotree").jstree("get_parent", nodeid);
      //const parentobj = $("#videotree").jstree("get_node", parentid);
      if (nodeType === "kc") {
        const kcbh = selected.node.data.kcbh;
        that.sharedService.emitChange({
          actiontype: "clicknode",
          frompage: that.frompage,
          data: {
            parentid: parentid,
            nodeid: kcbh,
            nodetype: nodeType,
          }
        });
      }
    });
  }


  /**
   * 节点是 kd /kc  -- kc是子节点
   */
  createtree_kd_kc(): void {
    const that = this;
    $("#videotree").jstree({
      "core": {
        "themes": {
          //如果使用 false默认主题则保留
          name: "default-dark",
          // 主题的CSS文件的URL，请保留该属性，就 false好像您已手动包含主题CSS（推荐）一样。您也可以将此设置为true 将尝试自动加载主题
          url: true,
          //所有jstree主题的位置-仅当 url 设置为时使用 true
          // 最终加载的css文件是  assets/jstreethemes/ 上面配置的name属性的值 /style.css【这里面的样式名要换成 上面配置的name属性的值】
          dir: "assets/jstreethemes",
          //指示是否显示连接点的布尔值
          dots: true,
          // 指示是否显示节点图标的布尔值
          icons: true,
          // 指示是否应显示节点省略号的布尔值-只能与容器上的固定with一起使用
          ellipsis: false,
          // 指示树背景是否为条纹的布尔值
          stripes: false,
          // 一个字符串（或boolean false），指定要使用的主题变体（如果主题支持变体）
          variant: false,
          //一个布尔值，指定主题的响应版本是否应在较小的屏幕上显示（如果主题支持）。默认为false.
          responsive: true,
        },
        data: function (node, cb) {
          if (node.id === "#") {
            that.kdNode(that.o.id, false, (data) => {
              cb(data);
            })
          } else {
            that.kcNode(node.id, true, (data) => {
              cb(data);
            });
          }
        }
      },
      "plugins": ["types"],
      "types": {
        "kd": {
          "icon": "fa fa-university"
        },
        "kc": {
          "icon": "fa fa-graduation-cap"
        }
      }
    }).on("select_node.jstree", function (e, selected) {
      const nodeType = selected.node.type;
      const nodeid = selected.node.id; // 如果节点是 kc, 那么 nodeid是 ""
      const parentid = $("#videotree").jstree("get_parent", nodeid);
      //const parentobj = $("#videotree").jstree("get_node", parentid);
      if (nodeType === "kc") {
        const kcbh = selected.node.data.kcbh;
        that.sharedService.emitChange({
          actiontype: "clicknode",
          frompage: that.frompage,
          data: {
            parentid: parentid,
            nodeid: kcbh,
            nodetype: nodeType,
          }
        });
      }
    });
  }

  /**
   * 节点 是   ksgljg/kd/kc/jt   jt是子节点
   */
  createtree_ksgljg_kd_kc_jt(): void {
    const that = this;
    $("#videotree").jstree({
      "core": {
        "themes": {
          //如果使用 false默认主题则保留
          name: "default-dark",
          // 主题的CSS文件的URL，请保留该属性，就 false好像您已手动包含主题CSS（推荐）一样。您也可以将此设置为true 将尝试自动加载主题
          url: true,
          //所有jstree主题的位置-仅当 url 设置为时使用 true
          // 最终加载的css文件是  assets/jstreethemes/ 上面配置的name属性的值 /style.css【这里面的样式名要换成 上面配置的name属性的值】
          dir: "assets/jstreethemes",
          //指示是否显示连接点的布尔值
          dots: true,
          // 指示是否显示节点图标的布尔值
          icons: true,
          // 指示是否应显示节点省略号的布尔值-只能与容器上的固定with一起使用
          ellipsis: false,
          // 指示树背景是否为条纹的布尔值
          stripes: false,
          // 一个字符串（或boolean false），指定要使用的主题变体（如果主题支持变体）
          variant: false,
          //一个布尔值，指定主题的响应版本是否应在较小的屏幕上显示（如果主题支持）。默认为false.
          responsive: true,
        },
        data: function (node, cb) {
          if (node.id === "#") {
            that.ksgljgkdNode("createtree_ksgljg_kd", that.o.id, false, (data) => {
              const rootNode = {
                id: that.o.id,
                type: that.o.type,
                text: that.o.text,
                data: {},
                state: {
                  opened: true
                },
                children: data
              };
              cb(rootNode);
            });
          } else {
            const nodetype = node.type;
            if (nodetype === "ksgljg") {
              that.ksgljgkdNode("createtree_ksgljg_kd", node.id, false, (data) => {
                cb(data);
              });
            } else if (nodetype === "kd") {
              that.kcNode(node.id, false, (data) => {
                cb(data);
              });
            } else if (nodetype === "kc") {
              const parentid = $("#videotree").jstree("get_parent", node.id);
              that.kcjtNode(parentid, node.id, true, (data) => {
                cb(data);
              });
            }
          }
        }
      },
      "plugins": ["types"],
      "types": {
        "ksgljg": {
          "icon": "glyphicon glyphicon-home"
        },
        "kd": {
          "icon": "fa fa-university"
        },
        "kc": {
          "icon": "fa fa-graduation-cap"
        },
        "jt": {
          "icon": "fa fa-video-camera"
        }
      }
    }).on("select_node.jstree", function (e, selected) {
      const nodeType = selected.node.type;
      const nodeid = selected.node.id;  //csid
      if (nodeType === "jt") {
        that.sharedService.emitChange({
          actiontype: "clicknode",
          frompage: that.frompage,
          data: {
            nodeid: nodeid,
            nodetype: nodeType,
            sipurl: selected.node.data.sipurl
          }
        });
      }
    });
  }


  /**
   * 接地那是 kd/kc/jt   jt是子节点
   */
  createtree_kd_kc_jt(): void {
    const that = this;
    $("#videotree").jstree({
      "core": {
        "themes": {
          //如果使用 false默认主题则保留
          name: "default-dark",
          // 主题的CSS文件的URL，请保留该属性，就 false好像您已手动包含主题CSS（推荐）一样。您也可以将此设置为true 将尝试自动加载主题
          url: true,
          //所有jstree主题的位置-仅当 url 设置为时使用 true
          // 最终加载的css文件是  assets/jstreethemes/ 上面配置的name属性的值 /style.css【这里面的样式名要换成 上面配置的name属性的值】
          dir: "assets/jstreethemes",
          //指示是否显示连接点的布尔值
          dots: true,
          // 指示是否显示节点图标的布尔值
          icons: true,
          // 指示是否应显示节点省略号的布尔值-只能与容器上的固定with一起使用
          ellipsis: false,
          // 指示树背景是否为条纹的布尔值
          stripes: false,
          // 一个字符串（或boolean false），指定要使用的主题变体（如果主题支持变体）
          variant: false,
          //一个布尔值，指定主题的响应版本是否应在较小的屏幕上显示（如果主题支持）。默认为false.
          responsive: true,
        },
        data: function (node, cb) {
          if (node.id === "#") {
            that.kdNode(that.o.id, false, (data) => {
              cb(data);
            })
          } else {
            const nodetype = node.type;
            if (nodetype === "kd") {
              that.kcNode(node.id, false, (data) => {
                cb(data);
              });
            } else if (nodetype === "kc") {
              const parentid = $("#videotree").jstree("get_parent", node.id);
              that.kcjtNode(parentid, node.id, true, (data) => {
                cb(data);
              });
            }
          }
        }
      },
      "plugins": ["types"],
      "types": {
        "kd": {
          "icon": "fa fa-university"
        },
        "kc": {
          "icon": "fa fa-graduation-cap"
        },
        "jt": {
          "icon": "fa fa-video-camera"
        }
      }
    }).on("select_node.jstree", function (e, selected) {
      const nodeType = selected.node.type;
      const nodeid = selected.node.id;  //csid
      if (nodeType === "jt") {
        that.sharedService.emitChange({
          actiontype: "clicknode",
          frompage: that.frompage,
          data: {
            nodeid: nodeid,
            nodetype: nodeType,
            sipurl: selected.node.data.sipurl
          }
        });
      }
    });
  }

  /**
   *
   * @param {string} id
   * @param {string} type
   * @param {string} cxlx    逗号隔开的字符串，lx顺序不限制   ksgljg,bzhkd,bms,zhzx
   * @param {Function} callback
   */
  cstreeNode(id: string, type: string, cxlx: string, callback: Function): void {
    const url = "ksyw/jcxxgl/slb";
    this.itservice.kwPtPost(url,
      {
        id: id,
        jdlx: type,
        cxlx: cxlx
      }
    ).subscribe(resp => {
      this.commservice.handResponse("get", url, resp, (flag, resp, flag2) => {
        if (flag) {
          if (flag2) {
            const treeList = resp.treeList;
            const childrenNodes = treeList.map((item) => {
              const jdlx = item.jdlx;
              const node = {
                id: item.id,
                type: jdlx,
                text: item.jdmc,
                data: {},
                children: true
              }
              if (jdlx === "zhzx" || jdlx === "bzhkd" || jdlx === "bms") {
                // 这几个是子节点
                node["children"] = false;
              } else {
                // ksgljg的，根据业务判断是否需要把县当成子节点
                const cxlxArr = cxlx.split(",");
                if (cxlxArr.length === 1) {
                  // cxlx是 ksgljg， 那么县就是子节点
                  const jdid = item.id;
                  if (jdid) {
                    const xiancode = jdid.toString().trim().substring(4, 6);
                    if (Number(xiancode)) {
                      node["children"] = false;
                    }
                  }
                }
              }
              return node;
            });
            callback(childrenNodes);
          } else {
            this.commservice.myswal("warning", resp, "", "ts", null);
            callback([]);
          }
        } else {
          this.initcstree("");
        }
      });
    });
  }

  async ksgljgkdNode(fucmc: string, nodeid: string, kdleaf: boolean, callback: Function) {
    let cbnodeArr: any[] = [];
    switch (fucmc) {
      case "createtree_ksgljg_kd":
        const ksgljgarr_createtree_ksgljg_kd = await this.ajax_gettreedata_ksgljg("ksyw/jcxxgl/slb", {
          id: nodeid,
          jdlx: "ksgljg",
          cxlx: "ksgljg"
        });
        const kdarr_createtree_ksgljg_kd = await  this.ajax_gettreedata_kd("ksyw/kwrycx/yglkdxxlb", {
          ksgljgid: nodeid,
          ksjhbh: this.s_ksjhbh,
          ccm: this.s_ksccbh
        }, kdleaf);
        cbnodeArr = ksgljgarr_createtree_ksgljg_kd.concat(kdarr_createtree_ksgljg_kd ? kdarr_createtree_ksgljg_kd : []);
        break;
    }
    callback(cbnodeArr);
  }

  kdNode(bzhkdid: string, kdleaf: boolean, callback: Function): void {
    const url = "ksyw/ywjk/wlkdzljkd";
    if (!this.s_ksjhbh || !this.s_ksccbh) {
      return
    }
    this.itservice.kwPtPost(url, {
      bzhkdid: bzhkdid,
      ksjhbh: this.s_ksjhbh,
      ccm: this.s_ksccbh
    }).subscribe(resp => {
      this.commservice.handResponse("get", url, resp, (arg1, resp, arg2) => {
        if (arg1) {
          if (arg2) {
            const wlkdzljkdLb = resp.wlkdzljkdLb;
            if (wlkdzljkdLb && wlkdzljkdLb.length) {
              const nodekdlist = wlkdzljkdLb.map((item) => {
                const kdbzid = resp.kdbzid;
                const kdbh = resp.kdbh;
                const kdmc = resp.kdmc;
                const kdjc = resp.kdjc;
                const kqbh = resp.kqbh;
                const node = {
                  id: kdbh,
                  type: 'kd',
                  text: kdmc,
                  data: {
                    bzhkdid: bzhkdid
                  },
                  children: !kdleaf
                }
                return node;
              });
              callback(nodekdlist);
            } else {
              callback([])
            }
          } else {
            callback([])
          }
        } else {
          this.kdNode(bzhkdid, kdleaf, callback);
        }
      });
    });
  }

  kcNode(kdbh: string, kcleaf: boolean, callback: Function): void {
    const url = "ksyw/ljkc/ljkcLb";
    this.itservice.kwPtPost(url, {
      ksjhbh: this.s_ksjhbh,
      ccm: this.s_ksccbh,
      kcmc: "",
      kcbh: "",
      kdbh: kdbh,
      pageNum: 1,
      pageSize: 9999
    }).subscribe(resp => {
      this.commservice.handResponse("get", url, resp, (arg1, resp, arg2) => {
        if (arg1) {
          if (arg2) {
            const ljkcList = resp.ljkcList;
            const childrenNodes = ljkcList.map((item) => {
              const node = {
                // 考场编号可能和 考点编号一样，所以，要处理
                //id: item.kcbh,
                type: "kc",
                text: item.kcmc,
                data: {
                  kcbh: item.kcbh
                },
                children: !kcleaf
              }
              return node;
            });
            callback(childrenNodes);
          } else {
            callback([]);
            this.commservice.myswal("warning", resp, "", "ts", null);
          }
        } else {
          this.kcNode(kdbh, kcleaf, callback);
        }
      });
    });
  }


  kcjtNode(kdbh: string, kcbh: string, jtleaf: boolean, callback: Function): void {
    const url = "spxc/spxc/tykhd/cxzdkcjtLb";
    this.itservice.kwPtPost(url, {
      ksjh: this.s_ksjhbh,
      ccm: this.s_ksccbh,
      kdbh: kdbh,
      kcbh: kcbh
    }).subscribe(resp => {
      this.commservice.handResponse("get", url, resp, (arg1, resp, arg2) => {
        if (arg1) {
          if (arg2) {
            const zdkcjtLb = resp.zdkcjtLb;
            if (zdkcjtLb && zdkcjtLb.length) {
              const childrenNodes = zdkcjtLb.map((item) => {
                const node = {
                  id: "",
                  type: "jt",
                  text: item.jtmc,
                  data: {
                    sipurl: item.sipurl
                  },
                  children: !jtleaf
                }
                return node;
              });
              callback(childrenNodes);
            } else {
              callback([])
            }
          } else {
            callback([])
          }
        } else {
          this.kcjtNode(kdbh, kcbh, jtleaf, callback);
        }
      });
    });
  }


  ksgljg_bmsNode(nodeid: string, nodelx: string, bmsleaf: boolean, callback: Function): void {
    const url = "ksyw/jcxxgl/slb";
    this.itservice.kwPtPost(url,
      {
        id: nodeid,
        jdlx: nodelx,
        cxlx: "ksgljg,bms"
      }
    ).subscribe(resp => {
      this.commservice.handResponse("get", url, resp, (flag, resp, flag2) => {
        if (flag) {
          if (flag2) {
            const treeList = resp.treeList;
            const childrenNodes = treeList.map((item) => {
              const jdlx = item.jdlx;
              const node = {
                id: item.id,
                type: jdlx,
                text: item.jdmc,
                data: {},
                children: true
              }
              if (jdlx === "bms") {
                node["children"] = !bmsleaf;
              }
              return node;
            });
            callback(childrenNodes);
          } else {
            this.commservice.myswal("warning", resp, "", "ts", null);
            callback([]);
          }
        } else {
          this.initcstree("");
        }
      });
    });
  }

  bmsjtNode(bmsid: string, jtleaf: boolean, callback: Function): void {
    const url = "bms/bmsjcxxgl/cxbmsjt";
    this.itservice.kwPtPost(url, {
      bmsbh: bmsid
    }).subscribe(resp => {
      this.commservice.handResponse("get", url, resp, (arg1, resp, arg2) => {
        if (arg1) {
          if (arg2) {
            const bmsJtList = resp.bmsJtList;
            const childrenNodes = bmsJtList.map((item) => {
              const node = {
                id: "",
                type: "jt",
                text: item.jtmc,
                data: {
                  sipurl: item.jtsipurl
                },
                children: !jtleaf
              }
              return node;
            });
            callback(childrenNodes)
          } else {
            callback([])
          }
        } else {
          this.bmsjtNode(bmsid, jtleaf, callback);
        }
      });
    });
  }

  ajax_gettreedata_ksgljg(url: string, reqobj: any): any {
    const that = this;
    return new Promise(function (resolve, reject) {
      that.itservice.kwPtPost(url, reqobj).subscribe(resp => {
        that.commservice.handResponse("get", url, resp, (flag, resp, flag2) => {
          if (flag) {
            if (flag2) {
              const treeList = resp.treeList;
              const childrenNodes = treeList.map((item) => {
                const node = {
                  id: item.id,
                  type: item.jdlx,
                  text: item.jdmc,
                  data: {
                    bzhkdid: ""
                  },
                  children: true
                }
                return node;
              });
              resolve(childrenNodes);
            } else {
              that.commservice.myswal("warning", resp, "", "ts", null);
              resolve([]);
            }
          } else {
            that.createtree_ksgljg_kd();
          }
        });
      });
    });

  }

  ajax_gettreedata_kd(url: string, reqobj: any, kdleaf: boolean): any {
    if (!this.s_ksjhbh || !this.s_ksccbh) {
      return
    }
    const that = this;
    return new Promise(function (resolve, reject) {
      that.itservice.kwPtPost(url, reqobj).subscribe(resp => {
        that.commservice.handResponse("get", url, resp, (arg1, resp, arg2) => {
          if (arg1) {
            if (arg2) {
              const yglkdxxList = resp.yglkdxxList;
              const childrenNodes = yglkdxxList.map((item) => {
                const node = {
                  id: item.kdbh,
                  type: item.jdlx,
                  text: item.jdmc,
                  data: {
                    bzhkdid: item.bzhkdid
                  },
                  children: !kdleaf
                }
                return node;
              });
              resolve(childrenNodes);
            } else {
              that.commservice.myswal("warning", resp, "", "ts", null);
              resolve([]);
            }
          } else {
            that.createtree_ksgljg_kd();
          }
        });
      });
    });
  }


  /**
   * frompage
   * treetype  cstree---场所  ywtree---和考试计划考试场次有关系
   * nodelx  树上要挂的节点类型
   * id
   * type
   * text
   * ksjhbh---(ywtree)
   * ksccbh --(ywtree)
   * clickroot----(cstree)默认是true, false--不点击根节点
   */
  @Input() o: any;

  constructor(private  itservice: MyhttpService,
              private  commservice: CommService,
              private sharedService: SharedService) {

  }

  ngOnInit() {
    this.treetype = this.o.treetype;
    if (this.treetype === "cstree") {
      this.initcstree("");
    } else if (this.treetype === "ywtree") {
      this.s_ksjhbh = this.o.ksjhbh;
      this.s_ksccbh = this.o.ksccbh;
      this.initywtree("");
    } else if (this.treetype == "csywtree") {
      this.initcsywtree("");
    }
  }

  ngOnChanges(changes: { [hs: string]: SimpleChange }) {
    const firstchange = changes.o.firstChange;
    if (!firstchange) {
      this.treetype = this.o.treetype;
      if (this.treetype === "cstree") {
        this.initcstree("refresh");
      } else if (this.treetype === "ywtree") {
        this.s_ksjhbh = this.o.ksjhbh;
        this.s_ksccbh = this.o.ksccbh;
        this.initywtree("refresh");
      } else if (this.treetype == "csywtree") {
        this.initcsywtree("refresh")
      }
    }
  }
}
